package com.qh.order.service.impl;

import com.qh.basic.util.LoginContext;
import com.qh.basic.util.pageList;
import com.qh.order.domain.OrderPetAcquisition;
import com.qh.order.dto.OrderDto;
import com.qh.order.mapper.OrderPetAcquisitionMapper;
import com.qh.order.query.OrderPetAcquisitionQuery;
import com.qh.order.service.IOrderPetAcquisitionService;
import com.qh.basic.service.impl.BaseServiceImpl;
import com.qh.org.domain.Employee;
import com.qh.org.domain.Shop;
import com.qh.org.mapper.EmployeeMapper;
import com.qh.org.mapper.ShopMapper;
import com.qh.pet.domain.Pet;
import com.qh.pet.domain.PetDetail;
import com.qh.pet.domain.SearchMasterMsg;
import com.qh.pet.mapper.PetMapper;
import com.qh.pet.mapper.SearchMasterMsgMapper;
import com.qh.pet.service.IPetService;
import com.qh.pet.service.impl.PetServiceImpl;
import com.qh.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private IPetService petService;

    @Override
    public pageList<OrderPetAcquisition> pageQuery(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        // 判断登录员工是那种角色
        //1.employee中shop_id为null，平台管理员
        Long shopId = employee.getShop_id();
        //2.店铺人员
        if (shopId!=null){
            Shop shop = shopMapper.loadOne(shopId);
            if(!shop.getAdminId().equals(employee.getId())){//店员 //注意：Long包装类不能用==判断，long才行
                //3.否则就为店员
                query.setEmpId(employee.getId());
            }
            //4.employee中shop_id不为null，通过shop_id查询店铺得到admin_id如果为当前登录管理员的id，就为店铺管理员
            query.setShopId(shopId);
        }
        //5.平台管理员
        return super.pageQuery(query);
    }

    /**
     * 确认订单
     * @param dto
     */
    @Override
    public void confirm(OrderDto dto) {
        //确认订单之后，接回宠物，1.平台需要录入一个宠物 --2.修改订单信息 --3.修改寻主消息状态
        Long orderId = dto.getId();
        BigDecimal money = dto.getMoney();//最终谈成的价格
        Integer payType = dto.getPaytype();
        //1.生成一个宠物
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadOne(orderId);
        SearchMasterMsg msg = searchMasterMsgMapper.loadOne(order.getSearchMasterMsgId());
        Pet pet = createPet(msg,money);
        /* 防止添加时富文本框消失 */
        PetDetail petDetail = new PetDetail();
        pet.setPetDetail(petDetail);

        petService.save(pet);
        //2.修改订单状态
        order.setState(1);
        order.setPrice(money);
        order.setPaytype(payType);
        order.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(order);
        //3.修改寻主消息状态
        msg.setState(2);//2--已完成
        searchMasterMsgMapper.update(msg);


    }

    /**
     * 对象转换
     * @param msg
     * @param money
     * @return
     */
    private Pet createPet(SearchMasterMsg msg, BigDecimal money) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(money);
        pet.setSaleprice(money);
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
//        pet.setState(0);//先不设置状态
        pet.setShopId(msg.getShopId());
        pet.setUserId(msg.getUserId());
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }

    /**
     * 取消订单
     * @param id
     */
    @Override
    public void cancel(Long id) {
        //订单取消之后，改状态，放回寻主池--将订单的状态改为取消-1
        //1.放回寻主池
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadOne(id);
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());

        //2.修改订单状态为-1
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
    }
}
